Previous Book Contents Book Index Next

Inside Macintosh: AppleScript Finder Guide /
Chapter 1 - Introduction to Finder Scripting


What Is Finder Scripting?

The Finder is a specialized application that controls the Macintosh desktop: the working area of your screen where you can use the mouse and the keyboard to open and close folders, manipulate files, and inspect or alter various aspects of the computer's operation. Finder scripting involves performing the same kinds of tasks, except that instead of triggering actions with the aid of the mouse and the keyboard, you trigger actions from scripts.

The Finder is both scriptable and recordable. A scriptable application is one that can respond to commands sent to it when another application, such as the Script Editor, runs a script. A recordable application is one that uses Apple events to report user actions for recording purposes. When recording is turned on, the Script Editor creates statements corresponding to any significant actions you perform in recordable applications, including actions you perform in the Finder. By recording or writing scripts that control the Finder, you can automate many file management and networking tasks that you would otherwise have to perform manually.

For example, the script that follows copies the items from a folder on the startup disk to a folder on a different disk. Instead of opening all the folders
by double-clicking them then dragging the contents of the AppleScript
folder to a storage disk, you can run the script.

tell application "Finder"   copy items of folder "AppleScript" of folder "Projects" of ¬
      startup disk to folder "Backups" of disk "Storage"end tell
This script consists of a three-line Tell statement that names the Finder application running on the local computer as the target application. (The ¬ symbol
is a "soft" return; it tells AppleScript to treat the line it's on and the line that follows as if they are a single line.) As far as AppleScript is concerned, the Finder is just like any other scriptable application.

Like other scriptable applications, the Finder defines commands that you
can use in scripts to describe actions. Some of these are standard application commands, such as Copy, Open, Print, and Move, that can be used with most scriptable applications. Others are commands unique to the Finder, such as Eject, Put Away, and Restart. The Finder also defines classes of objects on which actions can be performed, such as files, folders, and disks. Most of these object classes are unique to the Finder.

The copy command in the preceding example acts on the objects described by the reference items of folder "AppleScript" of folder "Projects" of startup disk. The folder AppleScript is located at the top level of the folder Projects, which located at the top level of the startup disk. The term
items refers to all objects in the AppleScript folder, including files, other folders, suitcases, and so on. Any object in the Finder can be identified from within
a script as long as its container and if necessary the container's containers
are also identified.

By using AppleScript features such as Repeat statements and other control statements, you can create more complex scripts that take different actions depending on the outcome of one or more tests. For example, the script that follows checks all the disks of a specified computer for the presence of a folder called Back Me Up. Whenever a disk contains such a folder, the script creates
a folder on a storage volume and copies the contents of Back Me Up to that folder. (The storage volume must be mounted on the remote computer for this script to work correctly.)

tell application "Finder" of machine "Macintosh IIci"   repeat with i in every disk in desktop
      if folder "Back Me Up" of i exists then
         set folderName to name of i & " " & ¬
            day of (current date) & " " & time of (current date)
         set newFolder to make folder at ¬
            disk "Storage" with properties {name:folderName}
         duplicate (items of folder "Back Me Up" of i) to newFolder
      end if
   end repeat
end tell
The Repeat statement in this script applies the statements it contains to every disk on the desktop of the computer named Macintosh IIci. The If statement within the Repeat statement checks each disk for the presence of a folder called Back Me Up. If the folder exists on a disk, the second line of the If statement sets the variable folderName to a string that consists of the disk's name,
the day of the month, and the time in seconds since the beginning of that day, thus ensuring that any other backup folders created from the same disk at a different time will have slightly different names. The rest of the If statement creates a new folder with the name assigned to folderName on a storage disk and asks the Finder to duplicate the items in the Back Me Up folder to the
new folder.

As you can see, scripts that control the Finder use familiar terms like folder, disk, and desktop as well as standard AppleScript terms like of, repeat, and tell. Chapter 2, "Finder Objects," describes all the terms the Finder defines for objects, and Chapter 3, "Finder Commands," describes the terms
it defines for commands. You can use these chapters as references when you need detailed information about specific objects or commands. However, the easiest way to learn how to use the scripting terminology defined by the Finder is to record your actions in the Finder and examine the resulting script.

The next section describes how to use record and edit scripts that control the Finder. When you see how the Finder uses its own terminology in recorded scripts, you can begin writing your own scripts from scratch.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996